package com.lgj.rocketmq.product.callback;

import org.apache.rocketmq.client.producer.SendCallback;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SendCallbackFactory implements ApplicationContextAware {

    private Map<String, AbstractSendCallBack> sendCallbackMap;

    public SendCallback callback(String messageType){
        return sendCallbackMap.get(messageType);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String,AbstractSendCallBack> callbackMap = applicationContext.getBeansOfType(AbstractSendCallBack.class);
        if (!callbackMap.isEmpty()){
            for (String beanName : callbackMap.keySet()){
                AbstractSendCallBack abstractSendCallBack = callbackMap.get(beanName);
                sendCallbackMap.put(abstractSendCallBack.getCallTag(),abstractSendCallBack);
            }
        }
    }
}
